global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/MP_import.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {
*Mann, Katja and Lukas Puettmann, 
*Github repository for “Benign Effects of Automation: New Evidence from Patent Texts,”
* The Review of Economic and Statistics, August 2021, pp. 1–45. 
*Accessed June 14 2021. https://github.com/lpuettmann/automation-patents
import delimited ${commondata_dir}/mann_puettmann/patents1.csv, clear

tempfile patents1
save `patents1'

import delimited ${commondata_dir}/mann_puettmann/patents2.csv, clear

append using `patents1', force

duplicates drop
tostring patent, gen(publn_nr)
drop patent

save ${commondata_dir}/mann_puettmann/puettmann_patent.dta, replace

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat